package com.jxtc.enterprise.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

@Configuration
public class RedisConfig {

    /**
     * RedisTemplate 序列化配置
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);

        // 设置键（Key）的序列化采用 StringRedisSerializer。
        template.setKeySerializer(new StringRedisSerializer());

        // 设置值（Value）的序列化采用 GenericJackson2JsonRedisSerializer。
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // 设置 Hash 键（Key）的序列化采用 StringRedisSerializer。
        template.setHashKeySerializer(new StringRedisSerializer());

        // 设置 Hash 值（Value）的序列化采用 GenericJackson2JsonRedisSerializer。
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        template.afterPropertiesSet();
        return template;
    }
}